package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.MsgTemplateDO;
import com.jinzhi.jzweb.service.MsgTemplateService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 消息模板
 * </pre>
 * <small> 2020-06-23 09:31:18 | lance</small>
 */
@Controller
@RequestMapping("/msgTemplate")
public class MsgTemplateController extends BaseController {
	@Autowired
	private MsgTemplateService msgTemplateService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:msgTemplate:msgTemplate")
	String MsgTemplate(Model model){
		List<MsgTemplateDO> msgList = msgTemplateService.findByKv("pid", 0);
		if (msgList != null){
			model.addAttribute("msg" , msgList);
		}
	    return "jzweb/msgTemplate/msgTemplate";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:msgTemplate:msgTemplate")
	public Result<Page<MsgTemplateDO>> list(MsgTemplateDO msgTemplateDTO){
		// 查询列表数据
		Page<MsgTemplateDO> page = msgTemplateService.selectPage(getPage(MsgTemplateDO.class),
					msgTemplateService.convertToEntityWrapper("pid" , msgTemplateDTO.getPid() , "id" , msgTemplateDTO.getId()).orderBy("id", false));

		List<MsgTemplateDO> list = page.getRecords();
		for (MsgTemplateDO msgTemp: list){
			UserDO user = userService.selectById(msgTemp.getCreateId());
			if (user != null){
				msgTemp.setCreateName(user.getUsername());
			}

		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:msgTemplate:add")
	String add(Model model){
		List<MsgTemplateDO> msgList = msgTemplateService.findByKv("pid", 0);
		if (msgList != null){
			model.addAttribute("msg" , msgList);
		}
	    return "jzweb/msgTemplate/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:msgTemplate:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MsgTemplateDO msgTemplate = msgTemplateService.selectById(id);
		List<MsgTemplateDO> msgList = msgTemplateService.findByKv("pid", 0);
		if (msgList != null){
			model.addAttribute("msg" , msgList);
		}
		model.addAttribute("msgTemplate", msgTemplate);
	    return "jzweb/msgTemplate/edit";
	}
	
	@Log("添加消息模板")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:msgTemplate:add")
	public Result<String> save( MsgTemplateDO msgTemplate){
		msgTemplate.setCreateId(this.getUserId());
		msgTemplate.setCreateTime(new Date());
		msgTemplateService.insert(msgTemplate);
        return Result.ok();
	}
	
	@Log("修改消息模板")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:msgTemplate:edit")
	public Result<String>  update( MsgTemplateDO msgTemplate){
		boolean update = msgTemplateService.updateById(msgTemplate);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除消息模板")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:msgTemplate:remove")
	public Result<String>  remove( Long id){
		msgTemplateService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除消息模板")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:msgTemplate:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		msgTemplateService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
